<template>
  <div>
    <el-row :gutter="20">
      <el-col :span="8">
        <div class="grid-content bg-purple">
          <!-- 首页user信息 -->
          <el-card shadow='hover'>
            <div class="userCard">
              <el-avatar :size="150" :src=imgUrl></el-avatar>
              <div class="userInfo">
                <p class="important-font">{{ user.userName }}</p>
                <p class="secondary-font">{{ user.role }}</p>
              </div>
            </div>
            <div class="login-info">
              <p>上次登录时间: {{ user.lastLoginTime }}</p>
            </div>
          </el-card>
          <!-- 首页表格 -->
          <el-card shadow='hover' class="tableInfo">
            <div slot="header">
              <span class="important-font">客户信息</span>
            </div>
            <div>
              <el-table
                  :data="customerInformation.tableData"
                  stripe
                  border
                  height="450px"
                  style="width: 100%">
                <el-table-column
                    prop="date"
                    label="日期"
                    width="120">
                </el-table-column>
                <el-table-column
                    prop="name"
                    label="姓名"
                    width="80">
                </el-table-column>
                <el-table-column
                    prop="address"
                    label="地址">
                </el-table-column>
              </el-table>
            </div>
          </el-card>
        </div>
      </el-col>
      <el-col :span="16">
        <!-- 六个订单信息 -->
        <div class="num">
          <el-card shadow='hover' v-for="item in orderIndicator.countData" :key="item.name"
                   :body-style="{ display: 'flex',padding: 0 }" class="OrderCard">
            <i class="icon" :class="'el-icon-'+item.icon" :style="{ background: item.color}"></i>
            <div>
              <p class="important-font">￥{{ item.value }}</p>
              <p class="secondary-font">{{ item.name }}</p>
            </div>
          </el-card>
        </div>
        <!-- 柱状图 -->
        <el-card style="height: 280px">
          <div style="height:280px;" ref="barEcharts">{{ initBarEcharts() }}</div>
        </el-card>
        <div class="num graph">
          <el-card style="width: 34%;height: 265px;marginRight: 1%">
            <div style="width: 100%;height: 265px;" ref="pieEcharts">{{ initPieEcharts() }}</div>
          </el-card>
          <el-card style="width:65%;height: 265px">
            <div style="height: 265px">
              <el-calendar v-model="value"></el-calendar>
            </div>
          </el-card>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import * as echarts from 'echarts';

export default {
  name: "IndexPage",
  data() {
    return {
      user: {
        userName: 'wanger',
        role: '管理员',
        lastLoginTime: '2022/07/06 18:16',
      },
      imgUrl: require('../assets/avatar.jpg'),
      customerInformation: {
        tableData: [],
      },
      orderIndicator: {
        countData: [],
      },
      bar: {
        title: {
          text: '销售表'
        },
        tooltip: {},
        legend: {
          data: []
        },
        xAxis: {
          data: []
        },
        yAxis: {},
        series: []
      },
      pie: {
        tooltip: {
          trigger: 'item'
        },
        legend: {
          top: '0%',
          left: 'left'
        },
        series: [
          {
            name: '订单信息',
            type: 'pie',
            radius: ['20%', '65%'],
            avoidLabelOverlap: false,
            label: {
              show: false,
              position: 'left'
            },
            labelLine: {
              show: false,
            },
            data: []
          }
        ]
      },
      value: new Date(),
    }
  },
  created() {
    this.$http.get('/myJson/homePage').then((resp) => {
      const d = resp.data.data;
      this.user = d.userInfo;
      this.customerInformation.tableData = d.customerInformation.list;
      this.orderIndicator.countData = d.indexes;

      this.bar.legend.data = d.marketBar.data.legend.data;
      this.bar.xAxis.data = d.marketBar.data.xAxis.data;
      this.bar.series = d.marketBar.data.series;

      this.pie.series[0].data = d.marketPie.data;
    })
  },
  methods: {
    //柱状图
    initBarEcharts() {
      // 新建一个promise对象
      let p = new Promise((resolve) => {
        resolve()
      })
      //然后异步执行echarts的初始化函数
      p.then(() => {
        //	此dom为echarts图标展示dom
        let myChart = echarts.init(this.$refs.barEcharts)
        let option = this.bar
        // 使用刚指定的配置项和数据显示图表。
        myChart.setOption(option);
      })
    },
    //饼图
    initPieEcharts() {
      let p = new Promise((resolve) => {
        resolve()
      })
      //然后异步执行echarts的初始化函数
      p.then(() => {
        let myChart = echarts.init(this.$refs.pieEcharts);
        let option = this.pie
        myChart.setOption(option);
      })
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="less" scoped>
.el-card__body {
  padding: 10px;
}

.userCard {
  height: 180px;
  display: flex;
  border-bottom: 2px solid #DCDFE6;
  border-radius: 2px;
}

.userInfo {
  width: auto;
  padding: 6% 0 0 6%;
}

.important-font {
  font-weight: 900;
  font-size: 25px;
}

.secondary-font {
  color: #909399;
}

.login-info {
  height: 40px;
  text-align: left;
  color: #909399;
}

.tableInfo {
  margin: 20px 0 0 0;
}

.OrderCard {
  margin: 0 0 30px 30px;
  border: #DCDFE6 1px solid;
  border-radius: 10px;

  i {
    width: 30%;
    line-height: 120px;
    font-size: 30px;
    color: #fff
  }

  div {
    width: 300px;
  }
}

.el-card {
  border: none;
}

.num {
  display: flex;
  flex-wrap: wrap;
}

.graph {
  margin: 15px 0 0 0;
}

.el-calendar {
  height: 265px;
}
</style>